@using NBitcoin
@model WalletSetupRequest

@{
    var method = ViewData["Method"];
    var isImport = method is WalletSetupMethod.Seed;
    var isHotWallet = method is WalletSetupMethod.HotWallet;
    var canUseHotWallet = ViewData["CanUseHotWallet"] is true;
    var canUseRpcImport = ViewData["CanUseRPCImport"] is true;
}

@if (!User.IsInRole(Roles.ServerAdmin))
{
    <div class="alert alert-warning">
        You are not an admin on this server. While you are able to import or generate a wallet via seed with
        your account, please understand that you are trusting the server admins not just with your
        <a href="https://docs.btcpayserver.org/Deployment/ThirdPartyHosting/#privacy-concerns" target="_blank" class="alert-link" rel="noreferrer noopener">privacy</a>
        but also with <a href="https://docs.btcpayserver.org/Deployment/ThirdPartyHosting/#trust-concerns" target="_blank" class="alert-link" rel="noreferrer noopener">trivial access to your funds.</a>
    </div>
}

<form id="generate-wallet-form" method="post" asp-action="GenerateWallet" asp-route-storeId="@Context.GetRouteValue("storeId")" asp-route-cryptoCode="@Context.GetRouteValue("cryptoCode")" asp-route-method="@method">
    @if (isImport)
    {
        <div class="form-group">
            <label asp-for="ExistingMnemonic" class="form-label" text-translate="true">Wallet Recovery Seed</label>
            <textarea asp-for="ExistingMnemonic" class="form-control font-monospace py-2" rows="2" autocomplete="off" autocorrect="off" autocapitalize="off"></textarea>
            <span asp-validation-for="ExistingMnemonic" class="text-danger"></span>
        </div>
    }

    <div class="form-group">
        <label asp-for="ScriptPubKeyType" class="form-label" text-translate="true">Address type</label>
        <select class="form-select w-auto" asp-for="ScriptPubKeyType">
            @if (ViewData["SupportSegwit"] is true)
            {
                <option value="@ScriptPubKeyType.Segwit" text-translate="true">Segwit (Recommended)</option>
                <option value="@ScriptPubKeyType.SegwitP2SH" text-translate="true">Segwit wrapped (Compatible with old wallets)</option>
                <option value="@ScriptPubKeyType.Legacy" text-translate="true">Legacy (Not recommended)</option>
            }
            else
            {
                <option value="@ScriptPubKeyType.Legacy" text-translate="true">Legacy</option>
            }

            @if (ViewData["SupportTaproot"] is true)
            {
                <option value="@ScriptPubKeyType.TaprootBIP86" text-translate="true">Taproot (For advanced users)</option>
            }
        </select>
        <span asp-validation-for="ScriptPubKeyType" class="text-danger"></span>
    </div>

    @if (isImport && canUseHotWallet)
    {
        <div class="form-group mt-4">
            <label class="d-flex align-items-center">
                <input type="checkbox" asp-for="SavePrivateKeys" class="btcpay-toggle me-3" />
                <div>
                    <label asp-for="SavePrivateKeys" class="form-check-label" text-translate="true">Is hot wallet</label>
                    <span asp-validation-for="SavePrivateKeys" class="text-danger"></span>
                    <p class="text-muted pt-2 mb-0" text-translate="true">If checked, each private key associated with an address generated will be stored as metadata and would be accessible to anyone with admin access to your server. Enable at your own risk!</p>
                </div>
            </label>
        </div>
    }
    else
    {
        <input asp-for="SavePrivateKeys" type="hidden" value="@isHotWallet" />

        @if (Model.CanUsePayJoin)
        {
            <div class="form-group mt-3">
                <label class="d-flex align-items-center">
                    <input type="checkbox" asp-for="PayJoinEnabled" class="btcpay-toggle me-3" />
                    <div>
                        <span text-translate="true">Enable PayJoin</span>
                        <span asp-validation-for="PayJoinEnabled" class="text-danger"></span>
                        <p class="text-muted pt-2 mb-0">
                            <span text-translate="true">PayJoin enhances the privacy for you and your customers. Enabling it gives your customers the option to use PayJoin during checkout.</span>
                            <a href="https://docs.btcpayserver.org/Payjoin/" target="_blank" rel="noreferrer noopener">
                                <vc:icon symbol="info" />
                            </a>
                        </p>
                    </div>
                </label>                 
            </div>
        }
    }
    
    <div class="mb-4">
        <button class="d-inline-flex align-items-center btn btn-link text-primary fw-semibold p-0" type="button" id="AdvancedSettingsButton" data-bs-toggle="collapse" data-bs-target="#AdvancedSettings" aria-expanded="false" aria-controls="AdvancedSettings">
            <vc:icon symbol="caret-down"/>
            <span class="ms-1" text-translate="true">Advanced settings</span>
        </button>
        <div id="AdvancedSettings" class="collapse @(string.IsNullOrEmpty(Model.Passphrase) && !Model.ImportKeysToRPC ? "" : "show")">
            <div class="pt-3">
                @if (isImport) // hide account option when creating a wallet
                {
                    <div class="form-group">
                        <label asp-for="AccountNumber" class="form-label" text-translate="true">Account</label>
                        <input asp-for="AccountNumber" class="form-control" style="max-width:10ch" min="0" step="1">
                        <span asp-validation-for="AccountNumber" class="text-danger"></span>
                    </div>
                }
                <div class="form-group">
                    <label asp-for="Passphrase" class="form-label" text-translate="true">Optional passphrase (BIP39)</label>
                    <input type="text" asp-for="Passphrase" class="form-control" autocomplete="off"/>
                    <span asp-validation-for="Passphrase" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label for="passphrase_conf" class="form-label" text-translate="true">Confirm passphrase</label>
                    <input type="text" name="passphrase_conf" id="passphrase_conf" class="form-control"/>
                    <span class="text-danger field-validation-valid" id="passphrase_conf_validation"></span>
                </div>

                @if (canUseRpcImport)
                {
                    <div class="form-group mt-4">
                        <label class="d-flex align-items-center">
                            <input type="checkbox" asp-for="ImportKeysToRPC" class="btcpay-toggle me-3"/>
                            <div>
                                <label asp-for="ImportKeysToRPC" class="form-check-label" text-translate="true">Import keys to RPC</label>
                                <span asp-validation-for="ImportKeysToRPC" class="text-danger"></span>
                                <p class="text-muted pt-2 mb-0">
                                    <span text-translate="true">Each address generated will be imported into the node wallet and you can view your balance through the node.</span>
                                    @if (isImport || isHotWallet)
                                    {
                                        <span text-translate="true">When this is enabled for a hot wallet, you are also able to use the node wallet to spend.</span>
                                    }
                                </p>
                            </div>
                        </label>
                    </div>
                }
            </div>
        </div>
    </div>
    @if (Model.AdditionalOptions is not null)
    {
        @foreach (var dictKeys in Model.AdditionalOptions)
        {
            <input type="hidden" asp-for="AdditionalOptions[dictKeys.Key]" />
        }
    }
    <button type="submit" class="btn btn-primary" id="Continue">@(isImport ? StringLocalizer["Continue"] : StringLocalizer["Create"])</button>
</form>

<script>
    document.getElementById("generate-wallet-form").addEventListener("submit", event => {
        const $form = event.currentTarget;

        if ($form.elements["passphrase_conf"].value !== $form.elements["Passphrase"].value) {
            const $validation = document.getElementById("passphrase_conf_validation");
            $validation.classList.remove("field-validation-valid");
            $validation.innerText = @StringLocalizer["Invalid passphrase confirmation"];
            event.preventDefault();
        }
    });
</script>
